package cn.zmit.kuxi.activity;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.LinearLayout;
import android.widget.TextView;
import cn.zmit.kuxi.R;
import cn.zmit.kuxi.contants.Url;
import cn.zmit.kuxi.image.ImageDisplayer;
import cn.zmit.kuxi.request.RequestTask;
import cn.zmit.kuxi.utils.BitmapUtils;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.xdroid.functions.request.JsonData;
import com.xdroid.request.extension.XDroidRequest;
import com.xdroid.request.extension.config.DataType;
import com.xdroid.request.extension.impl.OnRequestListenerAdapter;

/** * @author @parameter * @return */
public class ShareDetailsActivity extends BaseActivity {
	@ViewInject(R.id.tv_share_title)
	private TextView tv_share_title;
	@ViewInject(R.id.tv_share_data)
	private TextView tv_share_data;
	@ViewInject(R.id.tv_get_winUser)
	private TextView tv_get_winUser;
	@ViewInject(R.id.tv_joinNum)
	private TextView tv_joinNum;
	@ViewInject(R.id.tv_luckNum)
	private TextView tv_luckNum;
	@ViewInject(R.id.tv_public_date)
	private TextView tv_public_date;
	@ViewInject(R.id.tv_share_speech)
	private TextView tv_share_speech;
	@ViewInject(R.id.btn_goods_nowJoin)
	private Button btn_goods_nowJoin;
	@ViewInject(R.id.btn_goods_joinCarts)
	private Button btn_goods_joinCarts;
	private String title, goodsName, winUser, content;
	@ViewInject(R.id.ll_pic)
	private LinearLayout ll_pic;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setTitleView("晒单详情", true);
		setContentView(R.layout.activity_share_details);
		ViewUtils.inject(this);
		initView();
	}

	private void initView() {

		if (getTextFromBundle("flag").equals("往期晒单")) {
			title = getTextFromBundle("title");
			goodsName = getTextFromBundle("goodsName");
			winUser = getTextFromBundle("winUser");
			content = getTextFromBundle("content");
			String evaluate_id = getTextFromBundle("evaluate_id");
			Log.e("evaluate_id===", evaluate_id);
			tv_share_title.setText("第" + title + "期");
			tv_share_data.setText(goodsName);
			tv_get_winUser.setText("参与用户：" + winUser);
			tv_share_speech.setText(content);
			RequestTask.getInstance().getShareDetails(this, evaluate_id,
					new onRequestListener());
		}

	}

	private class onRequestListener extends OnRequestListenerAdapter<Object> {
		@Override
		public void onDone(XDroidRequest<?> request, String response,
				DataType dataType) {
			super.onDone(request, response, dataType);
			JsonData jsonData = JsonData.create(response);
			Log.e("response===", response);
			JsonData data = jsonData.optJson("info");
			if (data != null && data.length() > 0) {
				String buy_count = data.optString("buy_count");
				String lucky_num = data.optString("lucky_number");
				int num = Integer.parseInt(lucky_num);
				int luckNum = num;
				String time = data.optString("announceOn");
				List<String> mPicList = new ArrayList<String>();// 初始化图片路径
				String pictures = data.optString("pictures");
				if (!pictures.equals("")) {
					String[] pic = pictures.split("\\|");
					for (int j = 0; j < pic.length; j++) {
						Log.e("piclength===", pic.length + "");
						Log.e("pic[]===", pic[j] + "");
						mPicList.add(pic[j]);
					}
				}
				tv_joinNum.setText("本期参与：" + buy_count);
				tv_luckNum.setText("幸运号：" + luckNum + "");
				tv_public_date.setText("揭晓时间：" + time);
				// for (int i = 0; i < mPicList.size(); i++) {
				// final LinearLayout ll = new LinearLayout(context);
				// ImageView imageView = new ImageView(context);
				// imageView.setScaleType(ScaleType.FIT_XY);
				// ImageDisplayer.getInstance().dispalyImageWithRadius(
				// Url.IMAGE_PIC_ROOT + mPicList.get(i), imageView, 0);
				// LinearLayout.LayoutParams params = new
				// LinearLayout.LayoutParams(
				// 480, 640);
				// params.setMargins(10, 10, 10, 10);// 设置间距
				// imageView.setLayoutParams(params);
				// Log.e("url===", Url.IMAGE_PIC_ROOT + mPicList.get(i));
				// ll.addView(imageView);
				// ll.setGravity(Gravity.LEFT);
				// ll_pic.addView(ll);
				// }
				for (int i = 0; i < mPicList.size(); i++) {
					ImageView imageView = new ImageView(context);
					imageView.setScaleType(ScaleType.CENTER);
					imageView.setPadding(10, 10, 10, 10);
					com.lidroid.xutils.BitmapUtils utils = new com.lidroid.xutils.BitmapUtils(
							context);
					utils.display(imageView,
							Url.IMAGE_PIC_ROOT + mPicList.get(i));
					ll_pic.addView(imageView);
				}
			}
		}

		@Override
		public void onRequestPrepare() {
			super.onRequestPrepare();
		}

		@Override
		public void onRequestFailed(String error) {
			super.onRequestFailed(error);
			dismissProgressDialog();
		}
	}

	@OnClick({ R.id.btn_goods_joinCarts, R.id.btn_goods_nowJoin })
	private void OnClick(View v) {
		switch (v.getId()) {
		case R.id.btn_goods_joinCarts:// 加入购物车

			break;
		case R.id.btn_goods_nowJoin:// 立即参与

			break;

		default:
			break;
		}
	}
}
